#include <msgthread.h>

MsgThread::MsgThread(SOCKET receive){
    receive_sock = receive;
}

void MsgThread::run(){
    TransMsg trans_msg = {};
    while (true){
        cout << "recv ..." << endl;
        if (SockRecvMsg((char*)&trans_msg, receive_sock) <= 0){
            break;
        }else{
            //客户端有数据送达
            cout << "recv content:" << trans_msg.msg_content << endl;
            DealMsg(trans_msg);
            memset(&trans_msg, 0, sizeof(TransMsg));
        }
    }
    SockClose(receive_sock);
    return;
}

void MsgThread::DealMsg(TransMsg trans_msg){
    MsgType tp = trans_msg.msg_type;
    my_user_name = trans_msg.msg_to;
    switch (tp){
        case RSP_LOGIN  : emit LoginRSP(trans_msg.msg_content);break;
        case RSP_REGIST : emit RegistRSP(trans_msg.msg_content);break;
        case MESSAGE    : emit MsgComing(trans_msg.msg_from, trans_msg.msg_content);break;
        case RSP_HEARTB : emit HeartBRSP(trans_msg.msg_content);break;
        default         : cout << "i can't deal this type message";break;
    }
}

string MsgThread::GetName(){
    return my_user_name;
}

